//
//  ShouCang.m
//  PCAForWar
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ShouCang.h"
#import <DKNightVersion.h>
#import "DataBaseHandler.h"
#import "CellForVC2.h"
#import "ModelForVC2.h"
#import "VC2ForCell.h"
#import "VC2ViewController.h"

#define WIDTH  self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height

#define WIDTHE    [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTE  [UIScreen mainScreen].bounds.size.height / 667

@interface ShouCang () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrForlike;
@property (nonatomic, retain) VC2ViewController *vc2;
@property (nonatomic, retain) UIImageView *imageForLike;

@end

@implementation ShouCang

- (void)dealloc{
    
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_tableView release];
    [_arrForlike release];
    [_vc2 release];
    [_imageForLike release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 第一页button颜色
    self.arrForlike = [NSMutableArray array];
    
    self.arrForlike = [[DataBaseHandler shareDataBaseHandler] selectAll].mutableCopy;
    
    self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:22],}];
    
    [self createTableView];
    
}

#pragma mark 夜间模式
- (void)viewWillAppear:(BOOL)animated {
    
    // 添加如果没收藏的东西显示的图片
    self.arrForlike = [[DataBaseHandler shareDataBaseHandler] selectAll].mutableCopy;
    
    if (self.arrForlike.count == 0) {
        
        self.tableView.hidden = YES;
        self.imageForLike.hidden = NO;
        
    } else {
        
        self.tableView.hidden = NO;
        self.imageForLike.hidden = YES;
        
    }

    // 收藏button颜色
    self.arrForlike = [NSMutableArray array];
    
    self.arrForlike = [[DataBaseHandler shareDataBaseHandler] selectAll].mutableCopy;
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    [self.tableView reloadData];
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
    // 注册重用池
    
    [self.tableView registerClass:[CellForVC2 class] forCellReuseIdentifier:@"pool1"];
    
    self.imageForLike = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"WeChat_1457319907.jpeg"]];
    
    // 开启用户交互
    self.imageForLike.userInteractionEnabled = YES;
    
    self.imageForLike.frame = self.view.frame;
    
    [self.view addSubview:self.imageForLike];
    
    [_imageForLike release];
    
    [self createButtonTo];
    
}

#pragma mark 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForlike.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForVC2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    ModelForVC2 *model = [self.arrForlike objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
        return cell;
        
}

- (void)createButtonTo {
    
    UIButton *buttonTo = [UIButton buttonWithType:UIButtonTypeCustom];
    
    buttonTo.frame = CGRectMake(7 * WIDTHE, 395 * HEIGHTE, 360 * WIDTHE, 60 * HEIGHTE);
    
    buttonTo.backgroundColor = [UIColor grayColor];
    
    [self.imageForLike addSubview:buttonTo];
    
    [buttonTo setTitle:@"再去看看军事新闻吧" forState:UIControlStateNormal];
    
    buttonTo.titleLabel.font = [UIFont systemFontOfSize: 18.0];
    
    [buttonTo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [buttonTo addTarget:self action:@selector(BackToAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)BackToAction:(UIButton *)button {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

#pragma mark 收藏页面点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VC2ForCell *vc2Cell = [[VC2ForCell alloc] init];
    
    NSArray *array = nil;
    
    array = self.vc2.arrForJunPing;
    ModelForVC2 *model = [[ModelForVC2 alloc] init];
    
    model = [self.arrForlike objectAtIndex:indexPath.row];
    
    // 接收数据
    vc2Cell.Location = model.NewsUrl;
    
    vc2Cell.Title = model.Title;
    vc2Cell.NewsSummary = model.NewsSummary;
    vc2Cell.NewsCommentNum = model.NewsCommentNum;
    [self.navigationController pushViewController:vc2Cell animated:YES];
    
    [vc2Cell release];
    [model release];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
